package org.pyq.javaagent;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import sun.management.resources.agent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author pengYongQiang
 * @date 2020/8/30 13:39
 */
public class Demo {

    public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException, InterruptedException {

        System.out.println("args:"+ Arrays.toString(args));
       // String main  = args[0];
        String main = "com.paas.nesb.mesh.run.MeshRunApplication";
        if (args!=null&&args.length==1){
            main = args[0];
        }
        while (true) {
            //String main = "org.pyq.agent.Application";
            String id = selectProcessId(main);

            VirtualMachine attach = VirtualMachine.attach(id);
            //attach.loadAgent("E:\\idea-project-learn\\java-agent-Demo\\agent\\target\\agent-1.0-SNAPSHOT-shaded.jar");
            //  attach.loadAgent("E:\\idea-project-learn\\java-agent-Demo\\agent\\target\\agent-1.0-SNAPSHOT.jar");


            Scanner input = new Scanner(System.in);
            System.out.println("entry默认增强,reset 重置");
            System.out.println(input.hasNextLine());
            String next = input.nextLine();
            System.out.println("参数:"+next+">");
            //attach.loadAgent("E:\\idea-project-learn\\java-agent-Demo\\agent2\\target\\agent2-1.0-SNAPSHOT.jar",next);
            String path = ClassLoader.getSystemResource("").getPath();
            String s = path
                    + "agent2-1.0-SNAPSHOT.jar";
            //TODO linux 是/xxx/xx win是/E:/xxx
            s= s.substring(1);
            System.out.println(s);
            attach.loadAgent(s ,path+","+next);
        }
    }

    private static String selectProcessId(String main) throws IOException {
        Runtime run = Runtime.getRuntime();
        Process jps = run.exec("jps -l");
        InputStream inputStream = jps.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String temp = null;
        List<String> list = new ArrayList();
        while ((temp = bufferedReader.readLine()) != null) {
            list.add(temp);
            System.out.println(temp);
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        String id = null;
        if (main == null || "".equals(main.trim())) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入进程ID");
            id = input.next();
        } else {
            for (String s : list) {
                String[] s1 = s.split(" ");
                if (s1.length == 2) {
                    if (s1[1].equals(main)) {
                        System.out.println("\n已自动选择进程: " + s);
                        id = s1[0];
                    }
                }
            }
        }
        return id;
    }
}

